package com.example.rae.service.impl;

import com.example.rae.base.BaseApiService;
import com.example.rae.base.ResponseBase;
import com.example.rae.entity.UserEntity;
import com.example.rae.feign.IOrderFeign;
import com.example.rae.service.IOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author YangZeRong
 * @Date 2022/4/9 19:33
 * @Version 1.0
 */
@RestController
public class IOrderServiceImpl extends BaseApiService implements IOrderService {

    // 订单服务继承会员服务，通过feign调用
    @Autowired
    private IOrderFeign orderFeign;

    @RequestMapping("/orderToMember")
    @Override
    public String orderToMember(String name) {
        UserEntity member = orderFeign.getMember(name);
        return member.toString();
    }

    // 没有解决雪崩效应
    @RequestMapping("/orderToMemberUserInfo")
    @Override
    public ResponseBase orderToMemberUserInfo() {
        return orderFeign.getUserInfo();
    }

    // 订单服务接口
    @RequestMapping("/orderInfo")
    public ResponseBase orderInfo() {
        System.out.println("orderToMemberUserInfoHystrix:线程池名称：" + Thread.currentThread().getName());
        return setResultSuccess("成功", null);
    }

    // 解决雪崩效应
    // hystrix 有两种方式配置保护服务 通过注解和接口形式
    // fallBackMethod:服务降级执行
    // hystrixCommand:默认开启线程池隔离方式，服务降级，服务熔断
    @HystrixCommand(fallbackMethod = "orderToMemberUserInfoHystrixFallBack")
    @RequestMapping("/orderToMemberUserInfoHystrix")
    public ResponseBase orderToMemberUserInfoHystrix() {
        System.out.println("orderToMemberUserInfoHystrix:线程池名称：" + Thread.currentThread().getName());
        return orderFeign.getUserInfo();
    }

    // hystrix第二种写法  使用类方式
    @RequestMapping("/orderToMemberUserInfoHystrixDemo2")
    public ResponseBase orderToMemberUserInfoHystrixDemo2() {
        System.out.println("orderToMemberUserInfoHystrix:线程池名称：" + Thread.currentThread().getName());
        return orderFeign.getUserInfo();
    }

    public ResponseBase orderToMemberUserInfoHystrixFallBack() {
        return setResultSuccess("降级", "返回一个友好提升：服务降级，服务器忙，稍后重试！");
    }
}
